/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package primephysique;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author AP
 */
public class LogIn extends javax.swing.JPanel {

    /**
     * Creates new form LogIn
     */
    private JFrame parent;

    public LogIn(JFrame parent) {
        this.parent = parent;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbUserName = new javax.swing.JLabel();
        lbPassword = new javax.swing.JLabel();
        btnLogIn = new javax.swing.JButton();
        btnGuest = new javax.swing.JButton();
        pwPassword = new javax.swing.JPasswordField();
        txtUserName = new javax.swing.JTextField();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lbUserName.setText("User Name");
        add(lbUserName, new org.netbeans.lib.awtextra.AbsoluteConstraints(74, 42, -1, -1));

        lbPassword.setText("Password");
        add(lbPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 80, -1, -1));

        btnLogIn.setText("LogIn");
        btnLogIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLogInActionPerformed(evt);
            }
        });
        add(btnLogIn, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 123, -1, -1));

        btnGuest.setText("Guest");
        add(btnGuest, new org.netbeans.lib.awtextra.AbsoluteConstraints(172, 123, -1, -1));

        pwPassword.setColumns(12);
        pwPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pwPasswordActionPerformed(evt);
            }
        });
        add(pwPassword, new org.netbeans.lib.awtextra.AbsoluteConstraints(144, 77, -1, -1));

        txtUserName.setColumns(12);
        add(txtUserName, new org.netbeans.lib.awtextra.AbsoluteConstraints(144, 39, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void pwPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pwPasswordActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_pwPasswordActionPerformed

    private void btnLogInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLogInActionPerformed
        if ("".equals(txtUserName.getText())) {
            JOptionPane.showMessageDialog(null, "Enter your account !");
            return;
        }
        if ("".equals(pwPassword.getText())) {
            JOptionPane.showMessageDialog(null, "Enter your password !");
            return;
        }
        String user = txtUserName.getText();
        String pass = pwPassword.getText();
        int flag = 0;
        try {
            cdb = new ConnectDB();
            con = cdb.getConnection();
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            //check account
            String qr = "";
            String query = "SELECT Account FROM Staff ";
            rs = st.executeQuery(query);

            while (rs.next()) {
                qr = rs.getString(1);
                if (qr.trim().equals(user)) {
                    flag = 1;
                    break;
                }
            }
            if (flag != 1) {
                JOptionPane.showMessageDialog(null, "Account is not exist");

            } else {
                //check password
                query = "SELECT Password FROM Staff ";
                rs = st.executeQuery(query);
                while (rs.next()) {
                    qr = rs.getString(1);
                    if (qr.trim().equals(pass)) {
                        flag = 0;
                        break;
                    }
                }
                if (flag == 1) {
                    JOptionPane.showMessageDialog(null, "Password is invailid");
                } else {
                    JOptionPane.showMessageDialog(null, "Log in successfull");
                    // if log in successful then do some process
                    parent.setContentPane(new Statistic());
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_btnLogInActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnGuest;
    private javax.swing.JButton btnLogIn;
    private javax.swing.JLabel lbPassword;
    private javax.swing.JLabel lbUserName;
    private javax.swing.JPasswordField pwPassword;
    private javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables
    private ConnectDB cdb = null;
    private Connection con;
    private Statement st;
    private ResultSet rs;
}
